<?php

namespace App\Http\Requests;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Validation\Rule;

/**
 * 平台
 *
 * @Desc:验证请求类
 *
 * @author: zhx
 *
 * @Time: 2023/8/10 10:37
 */
class PlatformRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * 获取应用于请求的验证规则。
     *
     * @return array<string, \Illuminate\Contracts\Validation\Rule|array|string>
     */
    public function rules()
    {
        $id = $this->route('setmeal');

        return [
            'user_id' => 'required|integer',
            'app_id' => 'string|max:255',
            'number' => 'required|string|max:255',
            'logo' => 'required|string|max:255',
            'qr_code' => 'required|string|max:255',
            'name' => [
                'required',
                'string',
                'max:255',
                Rule::unique('admin_platform', 'name')->ignore($id),
            ],
            'category' => 'string|max:255',
            'remark' => 'required|string', // Add more validation rules for other fields...
        ];
    }

    public function messages(): array
    {
        return [
            'user_id.integer' => '用户ID必须为整数。',
            'user_id.required' => '用户ID不能为空。',
            'app_id.max' => '应用ID不得超过 :max 个字符。',
            'name.max' => '平台名称不得超过 :max 个字符。',
            'name.required' => '平台名称不能为空。',
            'name.unique' => '平台名称已存在。',
            'logo.required' => 'LOGO不能为空。',
            'qr_code.required' => '二维码不能为空。',
            'remark.required' => '平台描述不能为空。',
        ];
    }

    public function all($keys = null)
    {
        $data = parent::all($keys);
        // 在这里排除 _token 字段
        unset($data['_token']);
        $data['number'] = md5(\Illuminate\Support\Str::uuid());
        $data['user_id'] = Request()->user['id'];
        $data['status'] = -1; // 状态【-1.待选择套餐 0.停用|1.可用】
        $data['data4'] = '{"storage_disks":{"id":"1","name":"\u672c\u5730"}}'; // oss配置默认配置

        return $data;
    }

    protected function failedValidation(Validator $validator)
    {
        $error = $validator->errors()->all();
        throw new HttpResponseException(response()->json(['msg' => $error[0], 'code' => '500', 'data' => 'error'], 500));
    }
}
